import api from "../../../common/api"
import constant from '../../../common/constant';
var app = getApp();
Page({
  data: {
    disabledTabs: false,
    height: 0,
    top: 0,
    defaultTabSelect: "PENDING",
    tabSelect:'PENDING',
    list_item:[],
    status:'PENDING',
    tabList: [{
        id: "PENDING",
        label: "待完成"
      },
      {
        id: "COMPLETED",
        label: "已完成"
      },
      {
        id: "FAILURE",
        label: "已失效"
      }
    ],
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.gio('track', constant.Browse_Reward_Details_Page, {
      [constant.DetailEntr]: options.gotoActiveList
    })
    app.globalData.systemPromise.then(() => {
      this.getNavHeight();
      this.getInviteRecode()
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},


  // 设置导航栏的高度和返回按钮
  getNavHeight() {
    this.setData({
      height: app.globalData.navHeight,
      top: app.globalData.navTop,
    })
  },
  back() {
    wx.navigateBack({
      delta: 1,
    })
  },

  // tab改变获取数据
  onTabChange(e) {
    const {
      tabSelect
    } = this.data;
    let target = e.detail;
    if (tabSelect === target) return;
    this.setData({
      tabSelect: target,
      status:target
    });
     this.getInviteRecode();
  },
  // 邀请记录
  getInviteRecode() {
    this.setData({
      list_item: []
    });

    this.setData({ disabledTabs: true })
    this.selectComponent("#com-scroll-content").fetchHouse(1, ({ page }) => {
      return new Promise((resolve, reject) => {
        api.getInviteRecode({page,pageSize: 10,status: this.data.status}, ({data}) => {
          data.handleData = (rows) => {
            let tmpRow = []
            for (let i in rows) {
              let tmpRowItem = {}
              tmpRowItem.avatar = rows[i].wxHeadImg
              tmpRowItem.nickname = rows[i].nickName
              tmpRowItem.state = rows[i].state //pending进行中 fail失效 success成功
              tmpRowItem.data = rows[i].info
              if (!rows[i].newUser && rows[i].state== "FAILURE") {
                tmpRowItem.data = [{
                  type: rows[i].state,
                  time: rows[i].pullNewTime,
                  text: '该用户是老用户，邀请无效'
                }]
              }
              tmpRow.push(tmpRowItem)
            }
            this.setData({
              list_item: this.data.list_item.concat(tmpRow),
              disabledTabs: false,
            });
          };
          // data.content.rows.length = 0
          resolve(data);
        }, reject);
      });
    });
  },
})